package com.lsy.android.pingpang;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.lsy.android.pingpang.view.GameView;
import com.lsy.android.pingpang.view.OnePlayerView;
import com.lsy.android.pingpang.view.TwoPlayerView;
import com.umeng.analytics.MobclickAgent;

public class GameActivity extends Activity {

	GameView gameView;

	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		int gameSytle = getIntent().getIntExtra("gameStyle", 0);
		if (gameSytle == 0) {
			gameView = new OnePlayerView(this);
		} else if (gameSytle == 1) {
			gameView = new TwoPlayerView(this);
		} else {
			finish();
		}
		setContentView(gameView);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			showExitDialog();
		}
		return super.onKeyDown(keyCode, event);
	}

	private void showExitDialog() {
		gameView.GamePause();
		final Dialog dialog = new Dialog(this, R.style.exit_dialog);
		View view = LayoutInflater.from(this).inflate(R.layout.exit_dialog,
				null);
		dialog.setContentView(view);
		Button ok = (Button) view.findViewById(R.id.btn_ok);
		ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				GameActivity.this.finish();
			}
		});
		Button cancel = (Button) view.findViewById(R.id.btn_cancel);
		cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				gameView.GameContinue();
			}
		});
		dialog.show();
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

}
